package com.yanqu.road.server.cmd.wingroom.shareevent;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogWingRoomShareEventReward;
import com.yanqu.road.entity.wingroom.UserWingRoomData;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Date;

/**
 * @author ：zfr
 * @date ：2022/2/11 14:34
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GET_EVENT_REWARD_CROSS_RESP, desc = "跨服领取其他玩家厢房奖励返回")
public class GetEventRewardCrossRespCmd extends AbstractCommand {
    private WingRoomProto.GetOtherPlayerEventRewardRespMsg respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        respMsg = WingRoomProto.GetOtherPlayerEventRewardRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UserWingRoomData wingRoomData = player.getModule(WingRoomModule.class).getWingRoomData();
        wingRoomData.setPreGetEventRewardTimes(wingRoomData.getPreGetEventRewardTimes() - 1);
        if (respMsg.getRet() == 0) {
            wingRoomData.setGetEventRewardTimes(wingRoomData.getGetEventRewardTimes() + 1);
            player.getModule(WingRoomModule.class).userWingRoomDataSync();
            player.getModule(CurrencyModule.class).addCurrency(respMsg.getReward(), eLogMoneyType.WingRoom, eLogMoneyType.WingRoomGetEventReward);
            AutoLogMgr.add(new LogWingRoomShareEventReward(
                    player.getUserId(),
                    respMsg.getHostUserId(),
                    respMsg.getReward(),
                    new Date(respMsg.getEvent().getTriggerTime()),
                    new Date(respMsg.getEvent().getGetRewardTime()))
            );
            player.notifyListener(eGamePlayerEventType.WingRoomGetEventRewardTimes.getValue(),1);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(Protocol.U_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD, respMsg.toBuilder());
    }
}
